package com.mall.shopping.services;

import com.mall.shopping.ItemRecommendService;
import com.mall.shopping.constant.GlobalConstants;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.dal.entitys.Panel;
import com.mall.shopping.dal.entitys.PanelContentItem;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dal.persistence.PanelMapper;
import com.mall.shopping.dto.PanelContentItemDto;
import com.mall.shopping.dto.PanelDto;
import com.mall.shopping.dto.RecommendResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author Connor
 * @date 2020/12/21
 * @Version 1.0
 */
@Slf4j
@Component
@Service(interfaceClass = ItemRecommendService.class)
public class ItemRecommendServiceImpl implements ItemRecommendService {

    @Autowired
    private PanelMapper panelMapper;

    @Autowired
    private PanelContentMapper panelContentMapper;

    @Autowired
    private ContentConverter contentConverter;

    @Override
    public RecommendResponse queryItemRecommend() {
        Panel panel = null;
        List<Panel> panels = panelMapper.selectPanelContentById(GlobalConstants.RECOMMEND_PANEL_ID);
        if (panels != null) {
            panel = panels.get(0);
        }
        PanelDto panelDto = contentConverter.panel2Dto(panel);
        Set<PanelDto> panelDtos = new HashSet<>();
        List<PanelContentItem> panelContentItems =
                panelContentMapper.selectPanelContentAndProductWithPanelId(GlobalConstants.RECOMMEND_PANEL_ID);
        List<PanelContentItemDto> contentItemDtoList = contentConverter.panelContentItems2Dto(panelContentItems);
        panelDto.setPanelContentItems(contentItemDtoList);
        panelDtos.add(panelDto);

        RecommendResponse recommendResponse = new RecommendResponse();
        recommendResponse.setPanelContentItemDtos(panelDtos);
        recommendResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
        recommendResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        return recommendResponse;
    }
}
